﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using DataAnnotationsExtensions;
using OGS.Contracts;
using OGS.DataAccess;
using OGS.Utility;

namespace OGS.Web.Models.Admin
{
    public class SaveProductModel
    {
        /// <summary>
        /// The ID
        /// </summary>
        public int ID
        {
            get;
            set;
        }

        /// <summary>
        /// The Name
        /// </summary>
        [Display(Name = "Name (English)")]
        [Required(ErrorMessage = "Please enter name (english).")]
        public string EnglishName
        {
            get;
            set;
        }

        /// <summary>
        /// The Name
        /// </summary>
        [Display(Name = "Name (Bengali)")]
        [Required(ErrorMessage = "Please enter name (bengali).")]
        public string BengaliName
        {
            get;
            set;
        }

        /// <summary>
        /// The Name
        /// </summary>
        [Display(Name = "Name (Hindi)")]
        [Required(ErrorMessage = "Please enter name (hindi).")]
        public string HindiName
        {
            get;
            set;
        }

        /// <summary>
        /// The Description
        /// </summary>
        [Display(Name = "Description")]
        public string EnglishDescription
        {
            get;
            set;
        }

        /// <summary>
        /// The Description
        /// </summary>
        [Display(Name = "Description (bengali)")]
        public string BengaliDescription
        {
            get;
            set;
        }

        /// <summary>
        /// The Description
        /// </summary>
        [Display(Name = "Description (hindi)")]
        public string HindiDescription
        {
            get;
            set;
        }

        [Display(Name = "Parent Category")]
        public int ParentCategoryID
        {
            get;
            set;
        }

        /// <summary>
        /// The Category ID
        /// </summary>
        [Display(Name = "Category")]
        [Required(ErrorMessage = "Please select a category")]
        [Range(1, int.MaxValue, ErrorMessage = "Please select a category")]
        public int CategoryID
        {
            get;
            set;
        }

        /// <summary>
        /// The U R L
        /// </summary>
        [Required(ErrorMessage = "Please enter URL.")]
        public string Url
        {
            get;
            set;
        }

        /// <summary>
        /// The Brand ID
        /// </summary>
        [Display(Name = "Brand")]
        public int BrandID
        {
            get;
            set;
        }

        /// <summary>
        /// The S K U
        /// </summary>
        public string SKU
        {
            get;
            set;
        }

        /// <summary>
        /// The Quantity
        /// </summary>
        [Required(ErrorMessage = "Please enter Quantity.")]
        public string Quantity
        {
            get;
            set;
        }

        /// <summary>
        /// The U O M ID
        /// </summary>
        [Display(Name = "UOM")]
        [Required(ErrorMessage = "Please select a UOM")]
        [Range(1, int.MaxValue, ErrorMessage = "Please select a UOM")]
        public int UOMID
        {
            get;
            set;
        }

        /// <summary>
        /// The Avg Rating
        /// </summary>
        public int AvgRating
        {
            get;
            set;
        }

        /// <summary>
        /// The M R P
        /// </summary>
        [Range(1, int.MaxValue, ErrorMessage = "Please enter a valid MRP")]
        [DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)]
        public decimal MRP
        {
            get;
            set;
        }

        /// <summary>
        /// The Price
        /// </summary>
        [Range(1, int.MaxValue, ErrorMessage = "Please enter a valid Price")]
        [DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)]
        public decimal Price
        {
            get;
            set;
        }

        /// <summary>
        /// The Image_ Big
        /// </summary>
        public string Image_Big
        {
            get;
            set;
        }

        /// <summary>
        /// The Image
        /// </summary>
        public string Image
        {
            get;
            set;
        }

        /// <summary>
        /// Product Image File
        /// </summary>
        [Display(Name = "Image")]
        [ValidateImageFileAttribute(ErrorMessage = "Please upload valid image file")]
        public HttpPostedFileBase ProductImage { get; set; }

        /// <summary>
        /// Product Image File
        /// </summary>
        [Display(Name = "Big Image")]
        [ValidateImageFileAttribute(ErrorMessage = "Please upload valid image file")]
        public HttpPostedFileBase ProductBigImage { get; set; }

        /// <summary>
        /// Checks whether the Product Is Active
        /// </summary>
        public bool IsActive
        {
            get;
            set;
        }

        /// <summary>
        /// The Last Modified By
        /// </summary>
        public int LastModifiedBy
        {
            get;
            set;
        }

        /// <summary>
        /// Checks whether the Product Is Packaged Item
        /// </summary>
        public bool IsPackagedItem
        {
            get;
            set;
        }

        /// <summary>
        /// The Parent Category
        /// </summary>
        public List<Category> ParentCategories
        {
            get;
            set;
        }

        /// <summary>
        /// The Child Catgory
        /// </summary>
        public List<Category> Categories
        {
            get;
            set;
        }

        /// <summary>
        /// The Brand
        /// </summary>
        public List<Brand> Brands
        {
            get;
            set;
        }

        /// <summary>
        /// The UOM
        /// </summary>
        public List<UOM> UOMs
        {
            get;
            set;
        }

        public bool IsSuccess { get; set; }

        /// <summary>
        /// Constructor
        /// </summary>
        public SaveProductModel()
        {
            this.UOMs = new List<UOM>();
            this.Categories = new List<Category>();
            this.ParentCategories = new List<Category>();
            this.Brands = new List<Brand>();
        }

        /// <summary>
        /// Populate Brands
        /// </summary>
        public void PopulateBrands()
        {
            this.Brands = DABrand.Instance.GetBrands();
        }

        /// <summary>
        /// Populate Parent Categories
        /// </summary>
        public void PopulateParentCategories()
        {
            this.ParentCategories = DACategory.Instance.GetCategories(0);
        }

        /// <summary>
        /// Get Child Categories
        /// </summary>
        /// <param name="ParentCategoryID"></param>
        public void PopulateChildCategories(int ParentCategoryID)
        {
            this.Categories = DACategory.Instance.GetCategories(ParentCategoryID);
        }

        /// <summary>
        /// Populate Product
        /// </summary>
        /// <param name="ProductID"></param>
        public void Populate(int ProductID)
        {
            Product oProduct = DAProduct.Instance.GetProductDetails(ProductID);
            if (oProduct.ID > 0)
            {
                this.ParentCategoryID = oProduct.ParentCategoryID;
                this.CategoryID = oProduct.CategoryID;
                this.Url = oProduct.URL;
                this.BrandID = oProduct.BrandID;
                this.SKU = oProduct.SKU;
                this.Quantity = oProduct.Quantity;
                this.UOMID =  oProduct.UOMID;
                this.MRP = oProduct.MRP;
                this.Price = oProduct.Price;
                this.Image = oProduct.Image;
                this.Image_Big = oProduct.Image_Big;
                this.IsActive = oProduct.IsActive;

                this.EnglishName = oProduct.ProductLanguages[0].Name;
                this.BengaliName = oProduct.ProductLanguages[1].Name;
                this.HindiName = oProduct.ProductLanguages[2].Name;
                this.EnglishDescription = oProduct.ProductLanguages[0].Description;

                PopulateChildCategories(this.ParentCategoryID);
            }
        }


        /// <summary>
        /// Populate UOM
        /// </summary>
        public void PopulateUOMs()
        {
            this.UOMs = DAUOM.Instance.SearchUOM(new SearchAttributes());
        }

        /// <summary>
        /// Save Product
        /// </summary>
        /// <returns></returns>
        public int SaveProduct()
        {
            Product oProduct = new Product();
            oProduct.ID = this.ID;

            //Set Languages
            oProduct.ProductLanguages.Add(new ProductLanguage
            {
                LanguageID = (int)Language.English,
                Name = this.EnglishName.Trim(),
                Description = this.EnglishDescription != null ? this.EnglishDescription.Trim() : string.Empty
            });
            oProduct.ProductLanguages.Add(new ProductLanguage
            {
                LanguageID = (int)Language.Bengali,
                Name = this.BengaliName.Trim(),
                Description = this.EnglishDescription != null ? this.EnglishDescription.Trim() : string.Empty
            });
            oProduct.ProductLanguages.Add(new ProductLanguage
            {
                LanguageID = (int)Language.Hindi,
                Name = this.HindiName.Trim(),
                Description = this.EnglishDescription != null ? this.EnglishDescription.Trim() : string.Empty
            });

            oProduct.CategoryID = this.CategoryID;
            oProduct.URL = this.Url.Trim();
            oProduct.BrandID = this.BrandID;
            oProduct.SKU = this.SKU != null ? this.SKU.Trim() : string.Empty;
            oProduct.Quantity = this.Quantity;
            oProduct.UOMID = this.UOMID;
            oProduct.MRP = this.MRP;
            oProduct.Price = this.Price;
            oProduct.Image = this.Image != null ? this.Image.Trim() : string.Empty;
            oProduct.Image_Big = this.Image_Big != null ? this.Image_Big.Trim() : string.Empty;
            oProduct.IsPackagedItem = true;
            oProduct.IsActive = this.IsActive;
            oProduct.LastModifiedBy = Helper.GetCurrentUserID();

            int result = DAProduct.Instance.SaveProduct(oProduct);
            this.ID = oProduct.ID;
            return result;
        }
    }
}